<!-- 上传图片 -->
<template>
	<view class="upload-img" @click="uploadImg()">
		<slot name="upload">
			<img src="@/assets/image/uploadImg.png" v-if="disabled" />
			<img src="@/assets/image/icon45.png" v-else />
		</slot>
	</view>
</template>

<script setup>
	import {
		toRefs
	} from "vue";
	import {
		reactive,
		ref,
		getCurrentInstance,
		onMounted
	} from "vue"
	import {
		getAuthorization,
		setAuthorization
	} from "@/utils/auth.js"

	const prop = defineProps(['disabled'])
	const emits = defineEmits(['checkImg'])

	const state = reactive({
		formData: {
			address: ""
		},
	})

	let {
		formData
	} = toRefs(state)

	const uploadImg = () => {
		if (prop.disabled) {
			return
		}
		// 上传文件
		let tokenObj = getAuthorization("token") ? JSON.parse(getAuthorization("token")) : ""
		let token = tokenObj.AccessToken
		uni.chooseImage({
			count: 1, //默认9
			sizeType: ['original', 'compressed'], //可以指定是原图还是压缩图，默认二者都有
			sourceType: ['album', 'camera'], //从相册选择
			success: function(res) {
				uni.showLoading({
					title: "上传中..."
				})
				uni.uploadFile({
					url: '/api/app/api/G/user-heibai/v1/Files', //仅为示例，非真实的接口地址
					filePath: res.tempFilePaths[0],
					name: 'file',
					header: {
						"Authorization": "Bearer " + token
					},
					formData: {
						'groupCode': '99999999'
					},
					success: (uploadFileRes) => {
						let data = JSON.parse(uploadFileRes.data)
						if (data.code == 200) {
							emits("checkImg", data.data)
						}
					},
					fail: (error) => {
						uni.showToast({
							title: error.message
						})
					},
					complete: () => {
						uni.hideLoading()
					}
				});
			}
		});
	}
</script>

<style lang="scss" scoped>
	.upload-img {
		display: flex;
		align-items: center;

		img {
			width: 62rpx;
			height: 62rpx;
		}
	}
</style>
